package com.nmgsr.nettools.util;

import java.util.Locale;


public class LogUtil {

    public static String TAG = ConstantValue.PACKAGENAME;

    /**
     * 设置日志输出标记
     *
     * @param tag 日志标记
     */
    public static void setTag(String tag) {
//        d(tag, "Changing log tag to %s", tag);
        TAG = tag;
    }

    /**
     * 输出verbose级别日志
     *
     * @param format 日志格式
     * @param args   替换参数
     */
    public static void v(String format, Object... args) {
        if (ConstantValue.DEBUG) {
            android.util.Log.v(TAG, buildMessage(format, args));
        }
    }

    /**
     * 输出debug级别日志
     *
     * @param format 日志格式
     * @param args   替换参数
     */
    public static void d(String format, Object... args) {
        if (ConstantValue.DEBUG) {
            android.util.Log.d(TAG, buildMessage(format, args));
        }
    }

    /**
     * 输出info级别日志
     *
     * @param format 日志格式
     * @param args   替换参数
     */
    public static void i(String format, Object... args) {
        if (ConstantValue.DEBUG) {
            android.util.Log.i(TAG, buildMessage(format, args));
        }
    }

    /**
     * 输出error级别日志
     *
     * @param format 日志格式
     * @param args   替换参数
     */
    public static void e(String format, Object... args) {
        if (ConstantValue.DEBUG) {
            String msg = buildMessage(format, args);
            android.util.Log.e(TAG, msg);
        }
    }

    /**
     * 输出error级别日志
     *
     * @param tr     异常
     * @param format 日志格式
     * @param args   替换参数
     */
    public static void e(Throwable tr, String format, Object... args) {
        String msg = buildMessage(format, args);
        android.util.Log.e(TAG, msg, tr);
    }

    /**
     * 输出warn级别日志
     *
     * @param format 日志格式
     * @param args   替换参数
     */
    public static void w(String format, Object... args) {
        android.util.Log.w(TAG, buildMessage(format, args));
    }

    /**
     * Formats the caller's provided message and prepends useful i like
     * calling thread ID and method name.
     */
    private static String buildMessage(String format, Object... args) {
        String msg = (args == null || (args != null && args.length <= 0)) ? format : String.format(Locale.US, format, args);
        StackTraceElement[] trace = new Throwable().fillInStackTrace().getStackTrace();

        String caller = "<unknown>";
        // Walk up the stack looking for the first caller outside of VolleyLog.
        // It will be at least two frames up, so start there.
        for (int i = 2; i < trace.length; i++) {
            Class<?> clazz = trace[i].getClass();
            if (!clazz.equals(LogUtil.class)) {
                String callingClass = trace[i].getClassName();
                callingClass = callingClass.substring(callingClass.lastIndexOf('.') + 1);
                callingClass = callingClass.substring(callingClass.lastIndexOf('$') + 1);
                caller = callingClass + "." + trace[i].getMethodName();
                break;
            }
        }
//        return String.format(Locale.US, "[%d] %s: %s", Thread.currentThread().getId(), caller,msg);
        return String.format(Locale.US, "%s: %s", caller, msg);
    }
}
